<?php
/**
 * Created by PhpStorm.
 * User: 汪利东
 * Date: 2016/3/24
 * Time: 16:57
 */

namespace Admin\Model;


use Think\Model\RelationModel;

class AdminUserModel extends RelationModel
{
    protected $_link = [
        'group' => [
            'mapping_type' => self::HAS_MANY,
            'class_name'   => 'admin_group_has_user',
            'foreign_key'  => 'uid',
        ],
    ];


    protected function _after_select(&$result, $options)
    {
        parent::_after_select($result, $options); 

        if (!empty($options['link'])) {
            $this->getRelations($resultSet, $options['link']);
        }

        if ($resultSet) {
            foreach ($resultSet as &$result) {
                $this->_after_find($result, $options);
            }
        }

        foreach ($result as $k => $v) {
            foreach ($v['group'] as $key => $value) {
                $result[$k]['group'][$key] = M('admin_group')->where(['id' => $value['group_id']])->find();
            }
        }
    }

    protected function _after_find(&$result, $options)
    {
        parent::_after_find($result, $options); 

        if (!empty($options['link'])) {
            $this->getRelations($resultSet, $options['link']);
        }


        foreach ($result['group'] as $key => $value) {
            $result['group'][$key] = M('admin_group')->where(['id' => $value['group_id']])->find();
        }
    }
}